﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace A_link_Desktop
{
    public partial class MasterWindow : Form
    {
        private LoginModule m_login;

        private TextBox m_usernameBox;
        private TextBox m_passwordBox;
        private CheckBox m_rememberMeBox;
        private Button m_loginButton;

        public MasterWindow()
        {
            // Initialize the master window.
            InitializeComponent();

            // We don't want the login module in the 
            // static scope so let MasterWindow
            // handle it rather than Program.
            m_login = new LoginModule();

            // Since we're initializing we are starting the 
            // program and thus we need to log in.
            ShowLoginForm();
        }

        private void ShowLoginForm()
        {
            int x = 400;

            // Create the username textbox.
            m_usernameBox = new TextBox();
            m_usernameBox.Location = new Point(x - (m_usernameBox.Width / 2), 200);
            m_usernameBox.Parent = this;

            // Create the password textbox.
            m_passwordBox = new TextBox();
            m_passwordBox.Location = new Point(x - (m_passwordBox.Width / 2), 225);
            m_passwordBox.PasswordChar = '*';
            m_passwordBox.Parent = this;

            // Create the remember me checkbox.
            m_rememberMeBox = new CheckBox();
            m_rememberMeBox.Location = new Point(x - (m_rememberMeBox.Width / 2) + 10, 250);
            m_rememberMeBox.Text = "Remember Me";
            m_rememberMeBox.Parent = this;

            // Create the login button.
            m_loginButton = new Button();
            m_loginButton.Width = 110;
            m_loginButton.Location = new Point(x - (m_loginButton.Width / 2), 275);
            m_loginButton.Text = "Login";
            m_loginButton.Click += new EventHandler(m_loginButton_Click);
            m_loginButton.Parent = this;
        }
        
        private void RemoveLoginForm()
        {
            // Remove the username box.
            if (m_usernameBox != null)
            {
                m_usernameBox.Dispose();
                m_usernameBox = null;
            }

            // Remove the password box.
            if (m_passwordBox != null)
            {
                m_passwordBox.Dispose();
                m_passwordBox = null;
            }

            // Remove the remember me box.
            if (m_rememberMeBox != null)
            {
                m_rememberMeBox.Dispose();
                m_rememberMeBox = null;
            }

            // Remove the login button.
            if (m_loginButton != null)
            {
                m_loginButton.Dispose();
                m_loginButton = null;
            }
        }

        private void m_exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void m_loginButton_Click(object sender, EventArgs e)
        {
            // Do login check.
            if (true)
            {
                // Enabled logging out.
                m_logoutToolStripMenuItem.Enabled = true;
                
                // Remove the login form.
                RemoveLoginForm();
            }
            else
            {
                // Give feedback.
            }
        }

        private void m_logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Currently just shows the login form again.
            ShowLoginForm();
        }
    }
}